package com.luanye.enjoy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luanye.enjoy.entity.Wikipedia;
import com.luanye.enjoy.entity.WikipediaType;
import com.luanye.enjoy.mapper.WikipediaMapper;
import com.luanye.enjoy.service.WikipediaService;
import com.luanye.enjoy.service.WikipediaTypeService;
import com.luanye.enjoy.utils.BeanCopyUtils;
import com.luanye.enjoy.vo.ArticleDetail;
import com.luanye.enjoy.vo.ArticleVo;
import com.luanye.enjoy.vo.RespBean;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;

@Service("wikipediaService")
@AllArgsConstructor
public class WikipediaServiceImpl extends ServiceImpl<WikipediaMapper, Wikipedia> implements WikipediaService {

    private final WikipediaTypeService wikipediaTypeService;

    @Override
    public RespBean getWikiList(Integer pageNum, Integer limit, Integer typeId, Integer modeId) {
        LambdaQueryWrapper<Wikipedia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Wikipedia::getModeid,modeId);
        queryWrapper.eq(Wikipedia::getType,String.valueOf(typeId));

        WikipediaType Type  = wikipediaTypeService.getById(typeId);

        Page<Wikipedia> page = new Page<>(pageNum,limit);
        page(page, queryWrapper);
        List<Wikipedia> records = page.getRecords();
        List<ArticleVo> articleVos = BeanCopyUtils.copyBeanList(records, ArticleVo.class);

        for (ArticleVo vo: articleVos
        ) {
            vo.setTypename(Type.getTypename());
        }

        return RespBean.success(articleVos);
    }

    @Override
    public RespBean getArticleById(Long articleId) {
        Wikipedia byId = getById(articleId);
        ArticleDetail articleDetail = BeanCopyUtils.copyBean(byId, ArticleDetail.class);
        return RespBean.success(articleDetail);
    }
}
